/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.edu.web;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.cache.CacheUtils;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.edu.entity.EduProfessionalTitle;
import com.jeesite.modules.edu.service.EduProfessionalTitleService;

/**
 * 专业职称Controller
 * @author 赵俊飞
 * @version 2019-03-01
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/eduProfessionalTitle")
public class EduProfessionalTitleController extends BaseController {

	@Autowired
	private EduProfessionalTitleService eduProfessionalTitleService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public EduProfessionalTitle get(String professionalId, boolean isNewRecord) {
		return eduProfessionalTitleService.get(professionalId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("edu:eduProfessionalTitle:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduProfessionalTitle eduProfessionalTitle, Model model) {
		model.addAttribute("eduProfessionalTitle", eduProfessionalTitle);
		return "modules/edu/eduProfessionalTitleList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("edu:eduProfessionalTitle:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public List<EduProfessionalTitle> listData(EduProfessionalTitle eduProfessionalTitle) {
		if (StringUtils.isBlank(eduProfessionalTitle.getParentCode())) {
			eduProfessionalTitle.setParentCode(EduProfessionalTitle.ROOT_CODE);
		}
		if (StringUtils.isNotBlank(eduProfessionalTitle.getProfessionalName())){
			eduProfessionalTitle.setParentCode(null);
		}
		if (StringUtils.isNotBlank(eduProfessionalTitle.getRemarks())){
			eduProfessionalTitle.setParentCode(null);
		}
		List<EduProfessionalTitle> list = eduProfessionalTitleService.findList(eduProfessionalTitle);
		return list;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("edu:eduProfessionalTitle:view")
	@RequestMapping(value = "form")
	public String form(EduProfessionalTitle eduProfessionalTitle, Model model) {
		// 创建并初始化下一个节点信息
		eduProfessionalTitle = createNextNode(eduProfessionalTitle);
		model.addAttribute("eduProfessionalTitle", eduProfessionalTitle);
		return "modules/edu/eduProfessionalTitleForm";
	}
	
	/**
	 * 创建并初始化下一个节点信息，如：排序号、默认值
	 */
	@RequiresPermissions("edu:eduProfessionalTitle:edit")
	@RequestMapping(value = "createNextNode")
	@ResponseBody
	public EduProfessionalTitle createNextNode(EduProfessionalTitle eduProfessionalTitle) {
		if (StringUtils.isNotBlank(eduProfessionalTitle.getParentCode())){
			eduProfessionalTitle.setParent(eduProfessionalTitleService.get(eduProfessionalTitle.getParentCode()));
		}
		if (eduProfessionalTitle.getIsNewRecord()) {
			EduProfessionalTitle where = new EduProfessionalTitle();
			where.setParentCode(eduProfessionalTitle.getParentCode());
			EduProfessionalTitle last = eduProfessionalTitleService.getLastByParentCode(where);
			// 获取到下级最后一个节点
			if (last != null){
				eduProfessionalTitle.setTreeSort(last.getTreeSort() + 30);
				eduProfessionalTitle.setProfessionalId(IdGen.nextCode(last.getProfessionalId()));
			}else if (eduProfessionalTitle.getParent() != null){
				eduProfessionalTitle.setProfessionalId(eduProfessionalTitle.getParent().getProfessionalId() + "001");
			}
		}
		// 以下设置表单默认数据
		if (eduProfessionalTitle.getTreeSort() == null){
			eduProfessionalTitle.setTreeSort(EduProfessionalTitle.DEFAULT_TREE_SORT);
		}
		return eduProfessionalTitle;
	}

	/**
	 * 保存专业职称
	 */
	@RequiresPermissions("edu:eduProfessionalTitle:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated EduProfessionalTitle eduProfessionalTitle) {
		eduProfessionalTitleService.save(eduProfessionalTitle);
		return renderResult(Global.TRUE, text("保存专业职称成功！"));
	}
	
	/**
	 * 删除专业职称
	 */
	@RequiresPermissions("edu:eduProfessionalTitle:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(EduProfessionalTitle eduProfessionalTitle) {
		eduProfessionalTitleService.delete(eduProfessionalTitle);
		return renderResult(Global.TRUE, text("删除专业职称成功！"));
	}
	
	/**
	 * 获取树结构数据
	 * @param excludeCode 排除的Code
	 * @param isShowCode 是否显示编码（true or 1：显示在左侧；2：显示在右侧；false or null：不显示）
	 * @return
	 */
	@RequiresPermissions("edu:eduProfessionalTitle:view")
	@RequestMapping(value = "treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData(String excludeCode, String isShowCode) {

		List<Map<String, Object>> mapList = CacheUtils.get("CACHE_EDU_PROFESSIONAL_TITLE");
		if(!CollectionUtils.isEmpty(mapList)) {
			return mapList;
		}
		mapList = ListUtils.newArrayList();

		List<EduProfessionalTitle> list = eduProfessionalTitleService.findList(new EduProfessionalTitle());
		for (int i=0; i<list.size(); i++){
			EduProfessionalTitle e = list.get(i);
			// 过滤非正常的数据
			if (!EduProfessionalTitle.STATUS_NORMAL.equals(e.getStatus())){
				continue;
			}
			// 过滤被排除的编码（包括所有子级）
			if (StringUtils.isNotBlank(excludeCode)){
				if (e.getId().equals(excludeCode)){
					continue;
				}
				if (e.getParentCodes().contains("," + excludeCode + ",")){
					continue;
				}
			}
			Map<String, Object> map = MapUtils.newHashMap();
			map.put("id", e.getId());
			map.put("pId", e.getParentCode());
			map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getProfessionalName(), e.getProfessionalName()));
			mapList.add(map);
		}
		CacheUtils.put("CACHE_EDU_PROFESSIONAL_TITLE", mapList);
		return mapList;
	}

	/**
	 * 修复表结构相关数据
	 */
	@RequiresPermissions("edu:eduProfessionalTitle:edit")
	@RequestMapping(value = "fixTreeData")
	@ResponseBody
	public String fixTreeData(EduProfessionalTitle eduProfessionalTitle){
		if (!UserUtils.getUser().isAdmin()){
			return renderResult(Global.FALSE, "操作失败，只有管理员才能进行修复！");
		}
		eduProfessionalTitleService.fixTreeData();
		return renderResult(Global.TRUE, "数据修复成功");
	}
	
}